require(['config'],()=>{
    require(['template','jquery','header','footer','page'],(template,$)=>{
        class List{
            constructor(){
                this.list()
                this.page()
                this.sort()
            }
            list(){
                $.get('http://rap2.taobao.org:38080/app/mock/258602/api/list3',(resp)=>{
                    const {list}=resp.body;
                    $('#ul').html(template('list',{list}))
                    this.list=list
                })
            }
            page(){
                window.currentPage = function currentPage(currentPage){
                    // $("#pager").click  this => window document, 执行的function是执行的window上的
                    // 触发页码数位置： Page/js/jquery.z-pager.js 64行       
                   console.log("当前页码数：" + currentPage);
                }

                $('.pager').on('click',function(){
                    $.get('http://rap2.taobao.org:38080/app/mock/258602/api/list3',(resp)=>{
                        const {list}=resp.body;
                        $('#ul').html(template('list',{list}))
                    })
                })

                $("#pager").zPager({
                    totalData: 50,
                    htmlBox: $('#wraper'),
                    btnShow: true,
                    ajaxSetData: false
                });
            }
            sort(){
                $('.sort button').on('click',()=>{
                   var sortArr =  this.list.sort(function(obj1,obj2){
                        var val1=obj1.price;
                        var val2=obj2.price;
                        if (val1 < val2) {
                            return -1;
                        } else if (val1 > val2) {
                            return 1;
                        } else {
                            return 0;
                        }     
                    })
                    $('#ul').html(template('list',{
                        list:sortArr
                    }))
                   
                })
                $('.sort button:eq(1)').on('click',()=>{
                    var sortArr1 =  this.list.sort(function(obj1,obj2){
                        var val1=obj1.price;
                        var val2=obj2.price;
                        if (val1 < val2) {
                            return 1;
                        } else if (val1 > val2) {
                            return -1;
                        } else {
                            return 0;
                        }     
                    })
                    $('#ul').html(template('list',{
                        list:sortArr1
                    }))
                })
                
            }




        }
        new List()
    })
})